<template>
	<view class="work-container">
		<!-- 轮播图 -->
		<uni-swiper-dot class="uni-swiper-dot-box" :info="data" :current="current" field="content">
			<swiper class="swiper-box" :current="swiperDotIndex" @change="changeSwiper">
				<swiper-item v-for="(item, index) in data" :key="index">
					<view class="swiper-item" @click="clickBannerItem(item)">
						<image :src="item.image" mode="aspectFill" :draggable="false" />
					</view>
				</swiper-item>
			</swiper>
		</uni-swiper-dot>

		<!-- 宫格组件 -->
		<uni-section title="工作台" type="line"></uni-section>
		<view class="grid-body">
			<uni-grid :column="4" :showBorder="false" @change="changeGrid">
				<uni-grid-item :index="0">
					<view class="grid-item-box">
						<uni-icons type="person-filled" size="30"></uni-icons>
						<text class="text">修改信息</text>
					</view>
				</uni-grid-item>
				<uni-grid-item :index="1">
					<view class="grid-item-box">
						<uni-icons type="staff-filled" size="30"></uni-icons>
						<text class="text">历史账单</text>
					</view>
				</uni-grid-item>
				<uni-grid-item :index="2">
					<view class="grid-item-box">
						<uni-icons type="wallet-filled" size="30"></uni-icons>
						<text class="text">用电分析</text>
					</view>
				</uni-grid-item>
				<uni-grid-item :index="3">
					<view class="grid-item-box">
						<uni-icons type="chat-filled" size="30"></uni-icons>
						<text class="text">通知公告</text>
					</view>
				</uni-grid-item>

			</uni-grid>
		</view>

		<uni-section title="账单缴费" type="line" :sub-title="formatTime(lastBillInfo.billDate)">
			<uni-row class="demo-uni-row">
				<uni-card padding="0" spacing="0">
					<uni-list>
						<uni-list-item :title="`本期用电量:${lastBillInfo.delUsage} `"></uni-list-item>
						<uni-list-item :title="`支付金额:${lastBillInfo.payAmounts} `"></uni-list-item>
					</uni-list>
				</uni-card>
			</uni-row>
			<uni-row class="demo-uni-row">
				<uni-col :span="12">
					<view class="demo-uni-col dark"></view>
				</uni-col>
				<uni-col :span="4">
					<view class="demo-uni-col dark"></view>
				</uni-col>
				<uni-col :span="6">
					<view class="demo-uni-col dark" v-show="!payStatus">
						<button type="warn" @click="payBill">支付</button>
					</view>
					<view class="demo-uni-col dark" v-show="payStatus">
						<button type="primary">已支付</button>
					</view>
				</uni-col>
				<uni-col :span="2">
					<view class="demo-uni-col dark"></view>
				</uni-col>
			</uni-row>
		</uni-section>
	</view>
</template>

<script>
	import {
		getLastBillInfo
	} from "@/api/system/bill"
	import {
		sendPayRequest,
		callBackSuccess
	} from "@/api/system/pay"
	export default {
		data() {
			return {
				// 支付状态
				payStatus: false,
				// 上月账单详情
				lastBillInfo: {},
				current: 0,
				swiperDotIndex: 0,
				data: [{
						image: '/static/images/banner/lk.jpg'
					},
					// {
					// 	image: '/static/images/banner/banner01.jpg'
					// },
					{
						image: '/static/images/banner/banner02.jpg'
					},
					{
						image: '/static/images/banner/banner03.jpg'
					}
				]
			}
		},
		mounted() {
			this.getLastBillInfo();
		},
		onPullDownRefresh(){
			this.getLastBillInfo();
		},
		methods: {
			// 支付接口
			payBill() {
				// var EnvUtils = plus.android.importClass('com.alipay.sdk.app.EnvUtils');
				// EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);

				const billInfoId = this.lastBillInfo.billInfoId;
				var paymentData = ""
				// 获取后端支付链接
				// sendPayRequest(billInfoId).then(resp => {
				// 	paymentData = resp
				// })
				// 调用支付接口
				uni.requestPayment({
					"provider": "alipay",
					"orderInfo": paymentData,
					success: (resp) => {
						if(resp.memo != "用户中途取消"){
							callBackSuccess(billInfoId);
							this.getLastBillInfo();
							this.payStatus=true;
							console.log("success" + JSON.stringify((resp)))
						}
					},
					fail: (resp) => {
						console.log("fail" + JSON.stringify((resp)))
					}
				})
			},
			// 格式化时间
			formatTime(time) {
				const date = new Date(time); // 将 ISO 8601 字符串转换为 Date 对象
				const year = date.getFullYear(); // 获取年份
				const month = date.getMonth() + 1; // 获取月份（注意月份是从 0 开始的，所以要加 1）
				return `${year}年${month}月电费账单`;
			},
			// 查询上月的账单
			getLastBillInfo() {
				const userId = this.$store.state.user.uid;
				getLastBillInfo(userId).then(resp => {
					this.lastBillInfo = resp
					console.log(this.lastBillInfo)
					this.payStatus = this.lastBillInfo.payMethod > 0
				})
			},
			// 跳转到修改信息界面
			handleToEditInfo() {
				this.$tab.navigateTo('/pages/mine/info/edit')
			},
			// 跳转到历史账单列表
			handleToBillList() {
				this.$tab.navigateTo('/pages/bill/index');
			},
			clickBannerItem(item) {
				console.info(item)
			},
			changeSwiper(e) {
				this.current = e.detail.current
			},
			changeGrid(e) {
				const index = e.detail.index;
				// 如果为修改信息
				if (index == 0) {
					this.handleToEditInfo();
				}
				// 历史账单
				else if (index == 1) {
					this.handleToBillList();
				}
				// this.$modal.showToast('模块建设中~')
			}
		}
	}
</script>

<style lang="scss">
	/* #ifndef APP-NVUE */
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #fff;
		min-height: 100%;
		height: auto;
	}

	view {
		font-size: 14px;
		line-height: inherit;
	}

	/* #endif */

	.text {
		text-align: center;
		font-size: 26rpx;
		margin-top: 10rpx;
	}

	.grid-item-box {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 15px 0;
	}

	.uni-margin-wrap {
		width: 690rpx;
		width: 100%;
		;
	}

	.swiper {
		height: 300rpx;
	}

	.swiper-box {
		height: 150px;
	}

	.swiper-item {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
		color: #fff;
		height: 300rpx;
		line-height: 300rpx;
	}

	@media screen and (min-width: 500px) {
		.uni-swiper-dot-box {
			width: 400px;
			/* #ifndef APP-NVUE */
			margin: 0 auto;
			/* #endif */
			margin-top: 8px;
		}

		.image {
			width: 100%;
		}
	}

	.demo-uni-col {
		height: 36px;
		border-radius: 5px;
	}
</style>